/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 匹配字典,接收目标的value，返回可选
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'element', 'tabHeight', 'configureCom', 'configure', 'form'], function (exports) {
    var $ = layui.jquery;
    var element = layui.element;
    var tabHeight = layui.tabHeight;
    var configureCom = layui.configureCom;
    var form = layui.form;
    var configure = layui.configure;
    var indexObj = {};//点击过的tab存储对象
    var tabList = '';//tab数据列表
    var btnList = '';//btn数据列表
    $('#view').on('click', '[lay-event="close"]', function (e) {
        e.preventDefault();
        configureCom.closeWind(true);
    });
    var baseTab = {
        init: function (data, targetIndex) {
            targetIndex = targetIndex || 0;
            tabList = data.tabList;
            btnList = data.btns;
            var html = ' <div class="layui-tab" lay-filter="demo">\n' +
                '        <ul class="layui-tab-title">\n' +
                tabFun(targetIndex, tabList) +
                '        </ul>\n' +
                '        <div class="layui-tab-content">\n' +
                tabContent(targetIndex, tabList) +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>';
            var btnHtml = '<div class="btnBox">\n' +
                '        <form>\n' +
                btnFun(btnList) +
                '        </form>\n' +
                '    </div>';
            $('#view').append(btnHtml).append(html);
            $('#view iframe').eq(targetIndex).attr('src', configureCom.nocache(tabList[targetIndex].url));
            indexObj['li' + targetIndex] = true;
            tabHeight.init();
            element.render();
        }
    };
    element.on('tab(demo)', function (res) {
        var className = 'li' + res.index;
        if (indexObj[className]) {
            return false;
        } else {
            indexObj[className] = true;
            $('#view iframe').eq(res.index).attr('src', configureCom.nocache(tabList[res.index].url));
        }
    });

    //按钮初始化
    function btnFun(list) {
        var html = '';
        $(list).each(function (index, item) {
            html += '<button class="layui-btn" lay-submit lay-event="' + item.event + '">' + item.name + '</button>';
        });
        return html;
    }

    //tab标签初始化
    function tabFun(targetIndex, list) {
        var html = '';
        var targetClass = '';
        $(list).each(function (index, item) {
            targetClass = targetIndex == index ? 'layui-this' : '';
            html += '<li class="' + targetClass + '" data-name="' + getNameByUrl(item.url) + '">' + item.title + '</li>'
        });
        return html;
    }

    //tab内容初始化
    function tabContent(targetIndex, list) {
        var html = '';
        var targetClass = '';
        $(list).each(function (index, item) {
            targetClass = targetIndex == index ? 'layui-show' : '';
            html += '<div class="layui-tab-item ' + targetClass + '">' +
                '<iframe id="' + getNameByUrl(item.url) + '" class="iframe' + index + '" src="" frameborder="0" style="width:100%;height:100%;"></iframe>' +
                '</div>';
        });
        return html;
    }

    //截断url获取名称
    function getNameByUrl(url) {
        var res = url.split('.html')[0].split('/')[4];
        return res;
    }

    exports('baseTab', baseTab);
});
